home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / cvs-1_3.lha / cvs-1.3 / lib / ftruncate.c < prev    next >
C/C++ Source or Header  |  1991-09-30  |  1KB  |  73 lines

  1. /* ftruncate emulations that work on some System V's.
  2.    This file is in the public domain. */
  3.  
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6.  
  7. #ifdef F_CHSIZE
  8. int
  9. ftruncate (fd, length)
  10.      int fd;
  11.      off_t length;
  12. {
  13.   return fcntl (fd, F_CHSIZE, length);
  14. }
  15. #else
  16. #ifdef F_FREESP
  17. /* The following function was written by
  18.    kucharsk@Solbourne.com (William Kucharski) */
  19.  
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23.  
  24. int
  25. ftruncate (fd, length)
  26.      int fd;
  27.      off_t length;
  28. {
  29.   struct flock fl;
  30.   struct stat filebuf;
  31.  
  32.   if (fstat (fd, &filebuf) < 0)
  33.     return -1;
  34.  
  35.   if (filebuf.st_size < length)
  36.     {
  37.       /* Extend file length. */
  38.       if (lseek (fd, (length - 1), SEEK_SET) < 0)
  39.     return -1;
  40.  
  41.       /* Write a "0" byte. */
  42.       if (write (fd, "", 1) != 1)
  43.     return -1;
  44.     }
  45.   else
  46.     {
  47.       /* Truncate length. */
  48.       fl.l_whence = 0;
  49.       fl.l_len = 0;
  50.       fl.l_start = length;
  51.       fl.l_type = F_WRLCK;    /* Write lock on file space. */
  52.  
  53.       /* This relies on the UNDOCUMENTED F_FREESP argument to
  54.      fcntl, which truncates the file so that it ends at the
  55.      position indicated by fl.l_start.
  56.      Will minor miracles never cease? */
  57.       if (fcntl (fd, F_FREESP, &fl) < 0)
  58.     return -1;
  59.     }
  60.  
  61.   return 0;
  62. }
  63. #else
  64. int
  65. ftruncate (fd, length)
  66.      int fd;
  67.      off_t length;
  68. {
  69.   return chsize (fd, length);
  70. }
  71. #endif
  72. #endif
  73.